package net.disk;

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.isunlive.kuaipan.KuaipanAPI;
import com.isunlive.kuaipan.KuaipanAPIFactory;
import com.isunlive.kuaipan.exception.KuaipanAuthExpiredException;
import com.isunlive.kuaipan.exception.KuaipanException;
import com.isunlive.kuaipan.exception.KuaipanIOException;
import com.isunlive.kuaipan.exception.KuaipanServerException;
import com.isunlive.kuaipan.modle.KuaipanFile;
import com.isunlive.kuaipan.modle.KuaipanHTTPResponse;
import com.isunlive.kuaipan.modle.KuaipanUser;
import com.isunlive.kuaipan.test.KPTestUtility;

public class KuaipanAccess
{
	private static KuaipanAPI api;
	private static int flag = 0;

	public static void main(final String[] args)
	{
		final KuaipanAccess kuaipan = new KuaipanAccess();
		// kuaipan.moveFile("/123/123.txt", "/123/321.txt");
		final List<KuaipanFile> b = new ArrayList<KuaipanFile>();
		kuaipan.queryChildren("/", b);
		for (int i = 0; i < b.size(); i++) {
			System.out.println(b.get(i).path);
			/*
			 * System.out.println(kuaipan.queryChildren("/", b)); System.out.println("dage wei wu"); for (int i = 0; i <
			 * b.size(); i++) { System.out.println(b.get(i).path); }
			 */
		}
	}

	public KuaipanAccess()
	{
		api = KuaipanAPIFactory.getInstance();
	}

	public void accountInfo()
	{
		try {
			final KuaipanUser user = api.accountInfo();
			System.out.println(user.quota_used);
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
	}

	public void createFolder(final String path)
	{
		try {
			api.createFolder(path);
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
	}

	public void delete(final String path)
	{
		try {
			api.delete(path);
		}
		catch (final KuaipanIOException e) {
			e.printStackTrace();
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
	}

	public String downloadFile(final String fromPath)
	{
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
		KuaipanHTTPResponse resp;
		String download_content = null;
		try {
			resp = api.downloadFile(fromPath, os, null);
			assertTrue(resp.code == 200);
			download_content = KPTestUtility.outputStream2String(os);
			os.close();
		}
		catch (final KuaipanIOException e) {
			e.printStackTrace();
		}
		catch (final KuaipanAuthExpiredException e) {
			e.printStackTrace();
		}
		catch (final KuaipanServerException e) {
			e.printStackTrace();
		}
		catch (final IOException e) {
			e.printStackTrace();
		}
		return download_content;
	}

	public void moveFile(final String fromPath, final String toPath)
	{
		try {
			api.move(fromPath, toPath);
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
	}

	public List<KuaipanFile> queryChildren(final String path, final List<KuaipanFile> b)
	{
		List<KuaipanFile> list = new ArrayList<KuaipanFile>();
		try {
			list = api.metadata(path, true).files;
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).type.equals("folder")) {
				if (path == "/") {
					queryChildren(path + list.get(i).name, b);
				}
				else {
					queryChildren(path + "/" + list.get(i).name, b);
				}
			}
			else {
				if (path == "/") {
					list.get(i).path = path + list.get(i).name;
				}
				else {
					list.get(i).path = path + "/" + list.get(i).name;
				}
				b.add(list.get(i));
			}
		}
		return b;
	}

	public void renameFile(final String fromPath, final String newname)
	{
		try {
			api.move(fromPath, newname);
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
	}

	public long totalbytes()
	{
		try {
			return api.accountInfo().quota_total;
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public void updateFile(final java.io.File file, final String path)
	{
		InputStream is;
		try {
			is = new FileInputStream(file);
			api.uploadFile(path, is, file.length(), true, null);
		}
		catch (final FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (final KuaipanServerException e) {
			e.printStackTrace();
		}
		catch (final KuaipanAuthExpiredException e) {
			e.printStackTrace();
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
	}

	public void uploadFile(final java.io.File file, final String path)
	{
		InputStream is;
		try {
			is = new FileInputStream(file);
			api.uploadFile(path, is, file.length(), false, null);
		}
		catch (final FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (final KuaipanServerException e) {
			e.printStackTrace();
		}
		catch (final KuaipanAuthExpiredException e) {
			e.printStackTrace();
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
	}

	public long usedbytes()
	{
		try {
			return api.accountInfo().quota_used;
		}
		catch (final KuaipanException e) {
			e.printStackTrace();
		}
		return 0;
	}
}
